package com.xk._02算法篇._06greedy.leetcode;

/**
 * https://leetcode.cn/problems/can-place-flowers/
 * @description: 605.种花问题
 * @author: xu
 * @date: 2022/10/18 16:12
 */
public class Test03 {
    public static void main(String[] args) {
        boolean b = new Test03().canPlaceFlowers1(new int[]{0, 0, 1, 0, 0}, 1);
        System.out.println(b);
    }

    /*
     * 思路：
     *  遍历数组，i为数组下标
     *      1.当前的flowers[i]==1，i后移两位
     *      2.当前的i+1<数组长度且flowerbed[i+1]==1，i后移三位
     *      3.1和2走完了，那么当前下标索引的花盘符合种花条件，n--，i后移两位
     */
    public boolean canPlaceFlowers1(int[] flowerbed, int n) {
        for (int i = 0; i < flowerbed.length; ) {
            if (flowerbed[i] == 1) i += 2;
            else if (i + 1 < flowerbed.length && flowerbed[i + 1] == 1) i += 3;
            else {
                if (n == 0) return true;
                //flowerbed[i] = 1;
                i += 2;
                n--;
            }
        }
        return n == 0;
    }
}
